package com.dragon.repository.mapper;

import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.dragon.repository.entity.Admin;
import com.dragon.repository.entity.Position;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 岗位表 Mapper 接口
 * </p>
 *
 * @author sy
 * @since 2023-06-14
 */
@Mapper
public interface PositionMapper extends BaseMapper<Position> {

    @Select("<script>" +
            "WITH RECURSIVE region_temp AS ( " +
            "SELECT " +
            "t.id,t.pid,0 level " +
            "FROM " +
            "dragon_department t " +
            "WHERE 1=1 " +
            "<if test='departmentId.size != 0 and departmentId.size != null '>" +
            " and  t.id IN  " +
            "<foreach collection = 'departmentId' item = 'item' open = '(' close = ')' separator = ','>" +
            "#{item}" +
            "</foreach>" +
            "</if>" +
            " UNION ALL " +
            "SELECT " +
            "t.id,t.pid,level+1 level " +
            "FROM " +
            "dragon_department t " +
            "INNER JOIN region_temp t2 ON t.id = t2.pid " +
            ") " +
            "select da.id,da.username,department.department_id from " +
            "(select dp.id,dp.department_id,dt.level from region_temp dt left join dragon_position dp on dp.department_id = dt.id and dp.management_state = 1 where 1=1 " +
//            "<if test='grade!=null or grade == \"\"'>" +
//            " and dp.department_id in " +
//            "<foreach collection = 'departmentId' item = 'item' open = '(' close = ')' separator = ','>" +
//            "#{item}" +
//            "</foreach>" +
//            "</if>" +
            ") department " +
            "left join dragon_admin_position dap on dap.position_id = department.id " +
            "left join dragon_admin da on da.id = dap.admin_id and da.deleted = 0 and da.state = 1 where position_id is not null order by department.level asc " +
            "<if test='grade!=null'>" +
            " limit 0,#{grade}" +
            "</if>" +
            "</script>")
    @InterceptorIgnore(tenantLine = "true")
    List<Admin> levelPermissions(List<Integer> departmentId, Integer grade);


    @Select("<script>" +
            "select da.id,da.username,dp.department_id from dragon_position dp " +
            "left join dragon_admin_position dap on dp.id = dap.position_id and dp.management_state = 1 " +
            "left join dragon_admin da on dap.admin_id = da.id where da.deleted = 0 and da.state=1 " +
            "<if test='departmentId!=null or departmentId == \"\"'>" +
            " and dp.department_id in " +
            "<foreach collection = 'departmentId' item = 'item' open = '(' close = ')' separator = ','>" +
            "#{item}" +
            "</foreach>" +
            "</if>" +
            "</script>")
    List<Admin> chooseDepartment(List<Integer> departmentId);
}
